package online.misscl.lsy.controller.back;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import online.misscl.lsy.controller.BaseController;
import online.misscl.lsy.entity.VaccineInformation;
import online.misscl.lsy.entity.VaccineSortHospital;
import online.misscl.lsy.entity.dto.VaccineInfoAddDTO;
import online.misscl.lsy.entity.dto.VaccineInfoQueryDTO;
import online.misscl.lsy.entity.dto.VaccineInfoUpdateDTO;
import online.misscl.lsy.entity.dto.vo.TableDataInfoVo;
import online.misscl.lsy.entity.dto.vo.VaccineInfoVO;
import online.misscl.lsy.entity.dto.vo.VaccineInformationVo;
import online.misscl.lsy.service.VaccineInformationService;
import online.misscl.lsy.service.VaccineSortHospitalService;
import online.misscl.lsy.util.AjaxResult;
import online.misscl.lsy.util.Dto2Entity;
import online.misscl.lsy.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * project name: IntelliJ IDEA
 * Date: 2023/3/18 21:42
 *
 * @PackageName: online.misscl.lsy.controller.back
 * @ClassName: BackVaccineInfoController
 * @Author: code mover
 * @Version:
 * @Description:
 */
@RestController
@RequestMapping("/back")
@Api(tags = "疫苗信息模块")
public class BackVaccineInfoController extends BaseController {

    @Autowired
    private VaccineInformationService vaccineInformationService;
    @Autowired
    private VaccineSortHospitalService vaccineSortHospitalService;

    @ApiOperation(value = "获取疫苗信息列表",notes = "")
    @GetMapping("/vaccineInfos")
    public TableDataInfoVo<List<VaccineInfoVO>> vaccineInfoList(VaccineInfoQueryDTO vaccineInfoQueryDTO){
        LambdaQueryWrapper<VaccineSortHospital> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        LambdaQueryWrapper<VaccineSortHospital> eq = lambdaQueryWrapper
                .eq(VaccineSortHospital::getHospitalId, vaccineInfoQueryDTO.getHospitalId())
                .eq(VaccineSortHospital::getVaccineSortId, vaccineInfoQueryDTO.getSortId());

        VaccineSortHospital one = vaccineSortHospitalService.getOne(eq);

        LambdaQueryWrapper<VaccineInformation> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<VaccineInformation> eq1 =
                queryWrapper.eq(VaccineInformation::getHopitalSortId, one.getId());

        List<VaccineInformation> vaccineInformations = vaccineInformationService.list(eq1);
        List<VaccineInfoVO> vaccineInfoVOS = new ArrayList<>();


        Dto2Entity.populateList(vaccineInformations,vaccineInfoVOS,VaccineInfoVO.class);

        Page pages = PageUtil.getPages(vaccineInfoQueryDTO.getPageNum(),
                vaccineInfoQueryDTO.getPageSize(), vaccineInfoVOS);

        return getDataTable(pages.getRecords(), pages.getTotal());
    }
    @ApiOperation(value = "根据id获取单个疫苗信息列表",notes = "")
    @GetMapping("/vaccineInfo/{vaccineId}")
    public AjaxResult getVaccineInfo(@PathVariable Integer vaccineId){
        return AjaxResult.success(vaccineInformationService.getById(vaccineId));
    }

    @ApiOperation(value = "删除疫苗信息",notes = "")
    @DeleteMapping("/vaccineInfo/{vaccineId}")
    public AjaxResult deleteVaccineInfo(@PathVariable Integer vaccineId){
        return toAjax(vaccineInformationService.removeById(vaccineId));
    }

    @ApiOperation(value = "添加疫苗信息",notes = "")
    @PostMapping("/vaccineInfos")
    public AjaxResult addVaccineInfo(@RequestBody @Valid VaccineInfoAddDTO vaccineInfoAddDTO){
        System.out.println(vaccineInfoAddDTO.toString());
        //查询医院分类id
        VaccineSortHospital vaccineSortHospital = new VaccineSortHospital();
        vaccineSortHospital.setVaccineSortId(vaccineInfoAddDTO.getVaccineSortId());
        vaccineSortHospital.setHospitalId(vaccineInfoAddDTO.getHospitalId());

        LambdaQueryWrapper<VaccineSortHospital> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(VaccineSortHospital::getVaccineSortId,vaccineInfoAddDTO.getVaccineSortId())
                .eq(VaccineSortHospital::getHospitalId,vaccineInfoAddDTO.getHospitalId());

        VaccineSortHospital one = vaccineSortHospitalService.getOne(lambdaQueryWrapper);

        if (one == null) return AjaxResult.error("不存在该医院或该疫苗分类");


        VaccineInformation vaccineInformation = new VaccineInformation();

        Dto2Entity.populate(vaccineInfoAddDTO,vaccineInformation);
        vaccineInformation.setHopitalSortId(one.getId());

        //根据疫苗名称判断是否存在
        MPJLambdaWrapper<VaccineInformation> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.select(VaccineInformation::getVaccineId)
                .leftJoin(VaccineSortHospital.class,VaccineSortHospital::getId,VaccineInformation::getHopitalSortId)
        .eq(VaccineInformation::getVaccineName,vaccineInformation.getVaccineName())
        .eq(VaccineSortHospital::getHospitalId, vaccineInfoAddDTO.getHospitalId())
        ;

        VaccineInformationVo one1 = vaccineInformationService.getOneByVacNameAndHosId(
                vaccineInformation.getVaccineName(),vaccineInfoAddDTO.getHospitalId());

        if (one1 != null){
            return AjaxResult.error("该疫苗已存在！请重新输入");
        }
        return toAjax(vaccineInformationService.save(vaccineInformation));
    }
    @ApiOperation(value = "修改疫苗信息",notes = "")
    @PutMapping("/vaccineInfos")
    public AjaxResult updateVaccineInfo(@RequestBody @Valid VaccineInfoUpdateDTO vaccineInfoUpdateDTO){
        VaccineInformation vaccineInformation = new VaccineInformation();
        Dto2Entity.populate(vaccineInfoUpdateDTO, vaccineInformation);
        return toAjax(vaccineInformationService.updateById(vaccineInformation));
    }



}
